using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.ComponentModel;
using System.Collections;

namespace ResourceBlender_Express {
    /// <summary>
    /// Represents an application in ResourceBlender
    /// </summary>
    public class RBApplication : INotifyPropertyChanged {
        private string id,
                       applicationName,
                       displayName,
                       defaultLanguage;
        private List<string> languages;
        private ObservableSortedDictionary<string, string> bundles;

        /// <summary>
        /// Gets or sets the ID.
        /// </summary>
        /// <value>The ID.</value>
        public string ID {
            get { return id; }
            set {
                id = value;
                OnPropertyChanged("ID");
            }
        }
        /// <summary>
        /// Gets or sets the name of the application.
        /// </summary>
        /// <value>The name of the application.</value>
        public string ApplicationName {
            get { return applicationName; }
            set {
                applicationName = value;
                OnPropertyChanged("ApplicationName");
            }
        }
        /// <summary>
        /// Gets or sets the display name.
        /// </summary>
        /// <value>The display name.</value>
        public string DisplayName {
            get { return displayName; }
            set {
                displayName = value;
                OnPropertyChanged("DisplayName");
            }
        }
        /// <summary>
        /// Gets or sets the default language.
        /// </summary>
        /// <value>The default language.</value>
        public string DefaultLanguage {
            get { return defaultLanguage; }
            set {
                defaultLanguage = value;
                OnPropertyChanged("DefaultLanguage");
            }
        }
        /// <summary>
        /// Gets or sets the language codes used by the application.
        /// </summary>
        /// <value>The languages.</value>
        public List<string> Languages {
            get { return languages; }
            set {
                languages = value;
                OnPropertyChanged("Languages");
            }
        }
        /// <summary>
        /// Gets or sets the bundles included in the application. <BundleID, Name>
        /// </summary>
        /// <value>The bundles.</value>
        public ObservableSortedDictionary<string, string> Bundles {
            get { return bundles; }
            set {
                bundles = value;
                OnPropertyChanged("Bundles");
            }
        }

        /// <summary>
        /// Initializes a new <see cref="RBApplication"/>.
        /// </summary>
        public RBApplication() {
            this.id = string.Empty;
            this.applicationName = string.Empty;
            this.displayName = string.Empty;
            this.defaultLanguage = string.Empty;
            this.languages = new List<string>();
            this.bundles = new ObservableSortedDictionary<string, string>(new Comparers.DictionaryValueComparer(), new Comparers.StringEqualityComparer());
        }

        /// <summary>
        /// Initializes a new <see cref="RBApplication"/>.
        /// </summary>
        /// <param name="reader">The reader.</param>
        public RBApplication(DbDataReader reader) {
            if(reader.FieldCount < 5) {
                throw new ArgumentException("Reader must contain all fields");
            }
            this.ID = reader["id"].ToString();
            this.ApplicationName = reader["name"].ToString();
            this.DisplayName = reader["displayname"].ToString();
            this.DefaultLanguage = reader["defaultlanguage"].ToString();
            this.Languages = new List<string>(reader["languages"].ToString().Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries));
            this.Bundles = new ObservableSortedDictionary<string, string>(new Comparers.DictionaryValueComparer(), new Comparers.StringEqualityComparer());
        }

        /// <summary>
        /// Creates a deep copy of this instance.
        /// </summary>
        /// <returns></returns>
        public RBApplication Clone() {
            RBApplication clone = new RBApplication();
            clone.ID = this.ID;
            clone.ApplicationName = this.ApplicationName;
            clone.DisplayName = this.DisplayName;
            clone.DefaultLanguage = this.DefaultLanguage;
            clone.Languages = this.Languages;
            clone.Bundles = this.Bundles;
            return clone;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string strPropertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
            }
        }
    }    
}
